﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Hotnet.Biz.NSBizCommon;
using Hotnet.Biz.NSBizVideo;
using Hotnet.Web.Utility;
using Hotnet.Web.MVCExtension;
using Hotnet.Entity.Enum;
using System.IO;
using Hotnet.Entity;
using Hotnet.Entity.Search;
using Component.Utilities;

namespace Hotnet.Web.Controllers.Admin
{
	public class AdminVideoGroupController : AdminBaseController
	{
		//
		// GET: /AdminVideoGroup/


		private new const int PAGE_SIZE = 10;
		private BizSourceFrom bizSourceFrom;
		private BizStar bizStar;
		private BizVideoXTag bizVideoXTag;
		private BizCastCharacter bizCastCharacter;
		private BizVideo bizVideo;
		private BizVideoChild bizVideoChild;
		private BizVideoCategory bizVideoCategory;
		private string videoChildCoverImageFolder = Hotnet.Biz.Properties.Settings.Default.VideoChildCoverImageFolder;
		private string videoCoverImageFolder = Hotnet.Biz.Properties.Settings.Default.VideoCoverImageFolder;

		public AdminVideoGroupController(BizVideoChild bizVideoChild,BizSourceFrom bizSourceFrom, BizStar bizStar, BizVideoXTag bizVideoXTag, BizCastCharacter bizCastCharacter, BizVideo bizVideo, BizVideoCategory bizVideoCategory)
		{
			this.bizSourceFrom = bizSourceFrom;
			this.bizStar = bizStar;
			this.bizVideoXTag = bizVideoXTag;
			this.bizCastCharacter = bizCastCharacter;
			this.bizVideo = bizVideo;
			this.bizVideoCategory = bizVideoCategory;
			this.bizVideoChild = bizVideoChild;
		}

		public ActionResult Index(int? pageNo, SearchVideoCondition searchObj)
		{
			int totalCount = 0;
			searchObj.IsGroup = true;
			VideoCategory category = bizVideoCategory.Get(searchObj.CategoryID.Value);
			ViewListVideo[] list = bizVideo.List(pageNo ?? 0, PAGE_SIZE, out totalCount, searchObj,searchObj.IsPublished);
			ViewData["PaginateHelper"] = PaginateHelper.ConstructPaginate(totalCount, pageNo ?? 0, PAGE_SIZE, "/AdminVideoGroup/Index/", searchObj);
			ViewData["CategoryID"] = searchObj.CategoryID;
			ViewData["CategoryName"] = category.Name;
			return View(list);
		}

		private void InitCreateUpdate(List<string> selectSourceFrom, List<string> selectStars, List<string> selectDirector)
		{
			var stars = bizStar.List<string>(x => x.StarName, false);
			SelectControl sourcefroms = new SelectControl() { ButtonValue = "请选择来源", ControlType = HtmlControlType.radio, Text = "Name", Value = "SourceFromID", SelectedValues = selectSourceFrom, EnumerableData = bizSourceFrom.List(EnumSystemCategory.视频), Title = "请选择来源", CallbackScriptFunction = "setSourceFrom", PageControlID = 1 };
			ViewData["SourceFrom"] = sourcefroms;
			SelectControl characters = new SelectControl() { ButtonValue = "请选择主演", ControlType = HtmlControlType.checkbox, Text = "StarName", Value = "StarID", SelectedValues = selectStars, EnumerableData = stars, Title = "请选择主演", CallbackScriptFunction = "setStar", PageControlID = 2 };
			ViewData["Star"] = characters;
			SelectControl directors = new SelectControl() { ButtonValue = "请选择导演", ControlType = HtmlControlType.radio, Text = "StarName", Value = "StarID", SelectedValues = selectDirector, EnumerableData = stars, Title = "请选择导演", CallbackScriptFunction = "setDirector", PageControlID = 3 };
			ViewData["Director"] = directors;
		}

		//
		// GET: /AdminVideoGroup/Create

		public ActionResult Create(int? categoryID)
		{
			VideoCategory category = bizVideoCategory.Get(categoryID ?? 0);
			ViewData["Title"] = "视频组创建";
			ViewData["CategoryID"] = categoryID;
			ViewData["CategoryName"] = category.Name;
			InitCreateUpdate(null,null,null);
			return View();
		}

		//
		// POST: /AdminVideoGroup/Create

		[AcceptVerbs(HttpVerbs.Post)]
		[ValidateInput(false)]
		public ActionResult Create(string tags, string castCharacters, int? previewYear, Video videoGroup, HttpPostedFileBase coverImage)
		{
			DateTime previewTime = new DateTime(previewYear ?? DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
			videoGroup.PreviewTime = previewTime;
			videoGroup.AddTime = DateTime.Now;
			videoGroup.IsGroup = true;
			videoGroup.IsPublished = (int)EnumPublishState.未发布;
			int groupID = bizVideo.Insert(tags, castCharacters, videoGroup);
			videoGroup.CoverImagePath = SaveImage(coverImage, groupID, videoCoverImageFolder);
			bizVideo.Update(videoGroup);

			return RedirectToAction("Index", new { categoryID = videoGroup.CategoryID });
		}

		//
		// GET: /AdminVideoGroup/Edit/5

		public ActionResult Edit(int id)
		{
			ViewData["Title"] = "视频组编辑";
			Video videoGroup = bizVideo.Get(id);
			//tags
			ViewData["Tags"] = bizVideoXTag.ListVideoTagsByString(id);
			//video 来源
			List<string> selectSourceFrom = new List<string>() { videoGroup.SourceFromID.ToString() };
			//video 导演
			List<string> selectDirector = new List<string>();
			if (videoGroup.Director != null)
				selectDirector.Add(videoGroup.Director.ToString());
			//video 主演
			List<string> characters = bizCastCharacter.ListVideoCastCharacters(id).Select(x => x.StarID.ToString()).ToList();
			//上映年份
			ViewData["previewYear"] = videoGroup.PreviewTime == null ? "" : videoGroup.PreviewTime.Value.Year.ToString();
			ViewData["CategoryName"] = bizVideoCategory.Get(videoGroup.CategoryID.Value).Name;
			ViewData["CategoryID"] = videoGroup.CategoryID;
			InitCreateUpdate(selectSourceFrom, characters, selectDirector);
			return View("Create",videoGroup);
		}

		//
		// POST: /AdminVideoGroup/Edit/5

		[AcceptVerbs(HttpVerbs.Post)]
		[ValidateInput(false)]
		public ActionResult Edit(int? id, string tags, string castCharacters, int? previewYear, HttpPostedFileBase coverImage, Video videoGroup)
		{
			DateTime previewTime = new DateTime(previewYear ?? DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
			videoGroup.PreviewTime = previewTime;
			videoGroup.VideoID = id ?? 0;
			videoGroup.CoverImagePath = SaveImage(coverImage, id.Value, videoCoverImageFolder);

			bizVideo.Update(tags, castCharacters, videoGroup);
			TempData["UpdateResult"] = "更新成功";
			return RedirectToAction("Edit", new { id = id });
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public bool Delete(int? id)
		{
			return bizVideo.DeleteVideoGroup(id ?? 0);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public bool DeleteInList(int? id)
		{
			bizVideo.Update(id ?? 0, x => new Video() { DefineType = (int)EnumDefineType.None });
			return true;
		}

		public ActionResult GroupVideoList(int? id)
		{
			Video group = bizVideo.Get(id ?? 0);
			VideoChild[] list=bizVideoChild.ListVideoChilds(id ?? 0);
			ViewData["GroupID"] = id;
			ViewData["GroupName"] = group.Name;
			ViewData["CategoryID"] = group.CategoryID;
			ViewData["CategoryName"] = bizVideoCategory.Get(group.CategoryID.Value).Name;
			return View(list);
		}

		public ActionResult CreateGroupVideo(int? groupID)
		{
			Video group = bizVideo.Get(groupID ?? 0);
			ViewData["GroupID"] = groupID;
			ViewData["GroupName"] = group.Name;
			ViewData["CategoryID"] = group.CategoryID;
			ViewData["CategoryName"] = bizVideoCategory.Get(group.CategoryID.Value).Name;

			ViewData["Title"] = "视频创建";
			ViewData["GroupID"] = groupID ?? 0;
			return View();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		[ValidateInput(false)]
		public ActionResult CreateGroupVideo(VideoChild video, HttpPostedFileBase coverImage)
		{
			video.AddTime = DateTime.Now;
			bizVideoChild.Insert(video);
			video.CoverImagePath = SaveImage(coverImage,video.VideoID,videoChildCoverImageFolder);
			if (!string.IsNullOrEmpty(video.CoverImagePath))
				bizVideoChild.Update(video);
			return RedirectToAction("GroupVideoList", new { id = video.GroupID });
		}

		public ActionResult EditGroupVideo(int? id)
		{
			
			VideoChild video = bizVideoChild.Get(id ?? 0);
			Video group = bizVideo.Get(video.GroupID);
			ViewData["GroupID"] = video.GroupID;
			ViewData["GroupName"] = group.Name;
			ViewData["CategoryID"] = group.CategoryID;
			ViewData["CategoryName"] = bizVideoCategory.Get(group.CategoryID.Value).Name;
			ViewData["Title"] = "视频编辑";
			return View("CreateGroupVideo", video);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		[ValidateInput(false)]
		public ActionResult EditGroupVideo(VideoChild video, HttpPostedFileBase coverImage)
		{
			video.CoverImagePath = SaveImage(coverImage, video.VideoID, videoChildCoverImageFolder);
			bizVideoChild.Update(video);
			TempData["UpdateResult"] = "更新成功";
			return RedirectToAction("EditGroupVideo", new {id=video.VideoID });
		}

		[AcceptVerbs( HttpVerbs.Post)]
		public bool DeleteGroupVideo(int? id)
		{
			return bizVideoChild.Delete(id ?? 0);
		}

		private string SaveImage(HttpPostedFileBase coverImage, int videoId,string folder)
		{
			if (coverImage == null || videoId == 0)
				return null;
			string fileName;
			string fileExtension = coverImage.FileName.Substring(coverImage.FileName.IndexOf("."));
			string savePath = StringUtility.GetSavePath(videoId, out fileName);
			string dirPath = AppDomain.CurrentDomain.BaseDirectory + folder + savePath;
			if (!Directory.Exists(dirPath))
				Directory.CreateDirectory(dirPath);
			savePath = folder + savePath + fileName + fileExtension;
			coverImage.SaveAs(AppDomain.CurrentDomain.BaseDirectory + savePath);
			return savePath;
		}

	}
}
